Imports System
Imports System.Data

Public Class DbInfo

#Region "Properties"

	Private _TotalSize As Integer
	Private _UnallocatedSpace As Integer

	Public ReadOnly Property TotalSize() As Integer
		Get
			Return _TotalSize
		End Get
	End Property

	Public ReadOnly Property UnallocatedSpace() As Integer
		Get
			Return _UnallocatedSpace
		End Get
	End Property

#End Region

#Region "Constructors"

	Public Sub New()

	End Sub

	Public Sub New(ByVal reader As IDataReader)

		If Not reader.IsClosed Then

			If reader.Read() Then
				_TotalSize = GetSize(reader("database_size").ToString())
				_UnallocatedSpace = GetSize(reader("unallocated space").ToString())
			End If

		End If

		If Not reader.IsClosed Then
			reader.Close()
		End If

	End Sub

#End Region

#Region "Parse sizes"

	Private Function GetSize(ByVal value As String) As Integer

		Dim splitted() As String = Nothing
		Dim multiplier As Integer
		Dim result As Integer

		value = value.ToUpper()

		If value.Contains("GB") Then
			splitted = value.Replace("GB", String.Empty).Trim().Split(".".ToCharArray())
			multiplier = 1024 * 1024
		ElseIf value.Contains("MB") Then
			splitted = value.Replace("MB", String.Empty).Trim().Split(".".ToCharArray())
			multiplier = 1024
		ElseIf value.Contains("KB") Then
			splitted = value.Replace("KB", String.Empty).Trim().Split(".".ToCharArray())
			multiplier = 1
		End If

		result = Convert.ToInt32(splitted(0)) * multiplier

		If splitted.Length > 0 Then
			result += Convert.ToInt32(Convert.ToInt32(splitted(1)) / 1024 * multiplier)
		End If

		Return result

	End Function

#End Region

End Class